<?php
class ModuleBase 
{
  /* SQL pour module base
   * select mod_module, mtr_controller, mtr_method from glbo_user as user, glbo_access_right as access, glbo_module as module, glbo_module_tree as tree where user.usr_id=access.usr_id AND access.mod_id=module.mod_id AND module.mod_id=tree.mod_id;
   */

  private $modules;
  
  private $current_module;
  
  private $current_controller;

  /**
   *
   * Recupère les modules accessibles du BO sous la forme d'un pseudo singleton
   * Pour éviter les appels à la BD
   */
  public function __construct(){
    if(empty($_SESSION['modules'])){
      $this->modules['test'] = array('test_MainController' => array('index'));
      $this->modules['frbCms'] = array('frbCms_MainController' => array('index'));
      $this->modules['frbMessage'] = array('frbMessage_MainController' => array('index', 'readMessage'));
      $this->modules['frbFlat'] = array('frbFlat_MainController' => array('index', 'appartements', 'planning'));
      //$this->modules = $this->test();
      $_SESSION['modules'] = $this->modules;
    }else{
      $this->modules = $_SESSION['modules'];
    }

  }

  /**
   *
   * Renvoie le nom des modules
   * @return Array tableau qui contient les noms des modules;
   */
  public function getModules(){
    $name = Array();
    foreach ($this->modules as $module => $a_controller ) {
      if($module!='globalbo'){
        $name[] = $module;
      }
    }
    return $name;
  }

  /**
   *
   * Vérifie si le module, controleur, méthode sont bien chargées
   * @param String $search_module le nom du module à rechercher
   * @param String $search_controller le nom du controleur à rechercher
   * @param String $search_method le nom de la méthode à rechercher
   * @return TRUE si les 3 paramètres représente bien un module, un controleur et une méthode chargée FALSE sinon
   */
  public function isLoad($search_module, $search_controller, $search_method){  
    foreach ($this->modules as $module => $a_controller ) {
      if($module == $search_module){
        //echo 'module == OK';
        foreach ($a_controller as $controller => $a_method) {
          if($controller == $search_controller)
          //echo 'controller == OK';
          foreach ($a_method as $method) {
            if($method == $search_method){
              //echo 'method == OK';
              return true;
            }
          }
        }
      }
    }
    return false;
  }
  
  public function test(){
  /*$r = Doo::db()->query('select mod_module as module, mtr_controller as controller, mtr_method as method from glbo_user as user, glbo_access_right as access, glbo_module as module, glbo_module_tree as tree where user.usr_id=access.usr_id AND access.mod_id=module.mod_id AND module.mod_id=tree.mod_id;');
    $r->execute();
    $a = $r->fetchAll();
    
    foreach ($a as $value) {
      //On est en présence d'un nouveau module, on ajoute tout le tuple
      if ($value['module']!=$this->current_module){
        $result[$value['module']] = array($value['controller'] => array($value['method']));
        $this->current_module = $value['module'];
      }else{
        if($value['controller']!=$this->current_controller){
          $result[$value['module']][$value['controller']] = array($value['method']);
        }else{
          $result[$value['module']][$value['controller']][] = $value['method'];
        }
      }
    }
    return $result;
    */
    //return $tab['test'] = array('MainController' => array('index'));
  }

}